package cn.imvc.app.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.imvc.app.entities.Dealer;
import cn.imvc.app.entities.DealerApply;
import cn.imvc.app.entities.DealerLevel;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.AccountModel;
import cn.imvc.app.models.DealerApplyModel;
import cn.imvc.app.models.DealerLevelModel;
import cn.imvc.app.models.DealerModel;
import cn.imvc.app.models.GoodsModel;
import cn.imvc.app.models.MemberModel;
import cn.imvc.util.Wechat;

@Controller
@RequestMapping("dealerApply")
public class DealerApplyControllerImpl implements DealerApplyController {

	@Autowired
	DealerApplyModel dealerApplyModel;

	@Autowired
	MemberModel memberModel;

	@Autowired
	DealerLevelModel dealerLevelModel;

	@Autowired
	AccountModel accountModel;

	@Autowired
	DealerModel dealerModel;
	@Autowired
	GoodsModel goodsModel;
	@Autowired
	Wechat wechat;

//	@RequestMapping("choose.html")
//	public String choose(Model model, String token) {
//		// 用户未登录状态直接跳转登陆页面
//		if (token == null || !memberModel.checkLogin(token)) {
//			return "redirect:/member/login.html";
//		}
//
//		// 通过token从缓存里取member
//		Member member = memberModel.getLoginMember(token);
//		member = memberModel.get(member.getId());
//
//		// 查看是否有未完成的订单
//		List<DealerApply> dealerApplyList = dealerApplyModel.checkUnFinishedOrder(member.getId());
//
//		Dealer dealer = member.getDealer();
//
//		// 获取能升级的level
//		int level = dealer == null ? 0 : dealer.getDealerLevel().getLevel();
//		List<DealerLevel> dealerLevelList = dealerLevelModel.findByLevelGreaterThan(level);
//
//		model.addAttribute("myDealerLevelMoney", dealer == null ? 0 : dealer.getDealerLevel().getJoinMoney());
//		model.addAttribute("dealerLevelList", dealerLevelList);
//		if (dealerApplyList.size() > 0) {
//			model.addAttribute("dealerApplyId", dealerApplyList.get(0).getId());
//		} else {
//		}
//
//		return "dealer-apply/choose";
//	}
//
//	@RequestMapping("choose.htm")
//	public String pcchoose(Model model, String token) {
//
//		// 用户未登录状态直接跳转登陆页面
//		if (token == null || !memberModel.checkLogin(token)) {
//			return "redirect:/member/login.htm";
//		}
//
//		// 通过token从缓存里取member
//		Member member = memberModel.getLoginMember(token);
//		member = memberModel.get(member.getId());
//
//		// 查看是否有未完成的订单
//		List<DealerApply> dealerApplyList = dealerApplyModel.checkUnFinishedOrder(member.getId());
//
//		Dealer dealer = member.getDealer();
//
//		// 获取能升级的level
//		int level = dealer == null ? 0 : dealer.getDealerLevel().getLevel();
//		List<DealerLevel> dealerLevelList = dealerLevelModel.findByLevelGreaterThan(level);
//
//		model.addAttribute("myDealerLevelMoney", dealer == null ? 0 : dealer.getDealerLevel().getJoinMoney());
//		model.addAttribute("dealerLevelList", dealerLevelList);
//		if (dealerApplyList.size() > 0) {
//			model.addAttribute("dealerApplyId", dealerApplyList.get(0).getId());
//		} else {
//		}
//
//		return "dealer-apply/pcchoose";
//	}

	@RequestMapping("saveOrder.json")
	@ResponseBody
	public Map<String, Object> saveOrder(Integer levelId, Integer dealerApplyId, String token) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			resultMap.put("code", "-1");
			resultMap.put("msg", "未登录");
			return resultMap;
		}
		// 未选择等级
		if (levelId == null && dealerApplyId == null) {
			resultMap.put("code", "0");
			resultMap.put("msg", "请选择等级");
			return resultMap;
		}

		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		member = memberModel.get(member.getId());
		// 保存订单
		DealerApply dealerApply = null;
		if (dealerApplyId == null) {
			List<DealerApply> dealerApplyList = dealerApplyModel.checkUnFinishedOrder(member.getId());
			if (dealerApplyList.size() > 0) {
				dealerApply = dealerApplyList.get(0);
			} else {
				dealerApply = dealerApplyModel.saveDealerApplyOrder(levelId, member);
			}
		} else {
			dealerApply = dealerApplyModel.get(dealerApplyId);
		}

		resultMap.put("code", 1);
		resultMap.put("msg", "保存订单成功");
		resultMap.put("orderCode", dealerApply.getCode());
		resultMap.put("orderId", dealerApply.getId());
		return resultMap;
	}

	@RequestMapping("cancelOrder.json")
	@ResponseBody
	public Map<String, Object> cancelOrder(String code, String token) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			resultMap.put("code", "-1");
			resultMap.put("msg", "未登录");
			return resultMap;
		}
		// 未选择等级
		if (code == null) {
			resultMap.put("code", "0");
			resultMap.put("msg", "订单不存在");
			return resultMap;
		}
		DealerApply dealerApply = dealerApplyModel.findByCode(code);
		if (dealerApply == null) {
			resultMap.put("code", "0");
			resultMap.put("msg", "订单不存在");
			return resultMap;
		}
		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		if (dealerApply.getMember().getId() != member.getId()) {
			resultMap.put("code", "0");
			resultMap.put("msg", "您没有权限这么操作");
			return resultMap;
		}

		dealerApplyModel.delete(dealerApply.getId());

		resultMap.put("code", "1");
		resultMap.put("msg", "取消订单成功");
		return resultMap;
	}

}
